package com.woniuxy.rms.service.impl;

import com.woniuxy.rms.dao.DynamicCommentsMapper;
import com.woniuxy.rms.dao.DynamicDetailMapper;
import com.woniuxy.rms.dao.DynamicMapper;
import com.woniuxy.rms.dao.DynamicTagsMapper;
import com.woniuxy.rms.dto.DynamicDTO;
import com.woniuxy.rms.service.DynamicService;
import com.woniuxy.rms.util.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/25 0025:下午 6:27  <br/>
 * Description:动态服务层
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DynamicServiceImpl implements DynamicService {

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private DynamicDetailMapper dynamicDetailMapper;

    @Autowired
    private DynamicCommentsMapper dynamicCommentsMapper;

    @Autowired
    private DynamicTagsMapper dynamicTagsMapper;

    @Override
    public List<DynamicDTO> findAll(Integer currentPage, Integer pageSize) {
        return dynamicMapper.findAll(currentPage,pageSize);
    }

    @Override
    public DataResult delById(Long id) {
        //删除动态点赞
        dynamicTagsMapper.deleteTagByDynmaicId(id);
        //删除动态评论
        dynamicCommentsMapper.deleteCommentByDynmaicId(id);
        //删除动态详情
        dynamicDetailMapper.deleteDetailByDynmaicId(id);
        //删除动态
        dynamicMapper.deleteByPrimaryKey(id);
        return DataResult.success();
    }

    @Override
    public DataResult batchDelByIds(String ids) {
        //解析动态id字符串
        List<Long> dynamicIds = Arrays.stream(ids.split(",")).map(s -> Long.parseLong(s)).collect(Collectors.toList());
        //删除动态点赞
        dynamicTagsMapper.deleteTagByDynmaicIds(dynamicIds);
        //删除动态评论
        dynamicCommentsMapper.deleteCommentByDynmaicIds(dynamicIds);
        //删除动态详情
        dynamicDetailMapper.deleteDetailByDynmaicIds(dynamicIds);
        //删除动态
        dynamicMapper.deleteByIds(dynamicIds);
        return DataResult.success();
    }

    @Override
    public List<DynamicDTO> findScopeTime(Date startTime, Date endTime,Integer currentPage,Integer pageSize) {
        return dynamicMapper.findScopeTime(startTime,endTime,currentPage,pageSize);
    }

    @Override
    public DataResult batchDelByTime(Date startTime, Date endTime) {
        if (startTime == null){
            return DataResult.failed("请选择开始时间！");
        }
        if (endTime == null){
            return DataResult.failed("请选择结束时间！");
        }
        List<Long> ids = dynamicMapper.findByTime(startTime,endTime);
        //删除动态点赞
        dynamicTagsMapper.deleteTagByDynmaicIds(ids);
        //删除动态评论
        dynamicCommentsMapper.deleteCommentByDynmaicIds(ids);
        //删除动态详情
        dynamicDetailMapper.deleteDetailByDynmaicIds(ids);
        //删除动态
        dynamicMapper.deleteByIds(ids);
        return DataResult.success();
    }
}